package com.study.agp.paging;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.paging.PositionalDataSource;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class MoviePositionalDataSource extends PositionalDataSource<Movie> {
    private static final String TAG = "MoviePositionalDataSource";
    @Override
    public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<Movie> callback) {
        String paramsStr = String.format(Locale.CHINA,
                "loadInitial -> requestedStartPosition: %d, requestedLoadSize: %d, pageSize: %d, placeholdersEnabled: %s",
                params.requestedStartPosition, params.requestedLoadSize, params.pageSize, params.placeholdersEnabled);
        Log.i(TAG, paramsStr);

        List<Movie> movieList = new LinkedList<>();
        for (int i = 0; i < params.requestedLoadSize; i++) {
            movieList.add(new Movie("电影：" + (params.requestedStartPosition + i)));
        }
        callback.onResult(movieList, params.requestedStartPosition, 100);
    }

    @Override
    public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<Movie> callback) {
        String paramsStr = String.format(Locale.CHINA,
                "loadRange -> startPosition: %d, loadSize: %d",
                params.startPosition, params.loadSize);
        Log.i(TAG, paramsStr);

        List<Movie> movieList = new LinkedList<>();
        for (int i = 0; i < params.loadSize; i++) {
            movieList.add(new Movie("电影：" + (params.startPosition + i)));
        }
        callback.onResult(movieList);
    }
}
